package com.ksd.web.controller.tool;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class NetworkConfig {
    public static void setIpAddress(String interfaceName, String ipAddress) {
        String command = String.format("sudo ip addr add %s dev %s", ipAddress, interfaceName);
        try {
            Process process = Runtime.getRuntime().exec(command);
            process.waitFor();
            System.out.println("IP address set successfully.");
        } catch (IOException | InterruptedException e) {
            System.err.println("Error setting IP address: " + e.getMessage());
        }
    }

    public static void setMacAddress(String interfaceName, String macAddress) {
        String command = String.format("sudo ip link set %s address %s", interfaceName, macAddress);
        try {
            Process process = Runtime.getRuntime().exec(command);
            process.waitFor();
            System.out.println("MAC address set successfully.");
        } catch (IOException | InterruptedException e) {
            System.err.println("Error setting MAC address: " + e.getMessage());
        }
    }

    public static void setGateway(String gatewayIp) {
        String command = String.format("sudo ip route add default via %s", gatewayIp);
        try {
            Process process = Runtime.getRuntime().exec(command);
            process.waitFor();
            System.out.println("Gateway set successfully.");
        } catch (IOException | InterruptedException e) {
            System.err.println("Error setting gateway: " + e.getMessage());
        }
    }

    public static void setNetworkConfig(){
        try {
            // 修改网口1的IP地址
            String command1 = "sudo ip addr add 192.168.1.10/24 dev eth0";
            executeCommand(command1);

            // 修改网口2的IP地址
            String command2 = "sudo ip addr add 192.168.2.10/24 dev eth1";
            executeCommand(command2);

            // 修改网口1的MAC地址
            String command3 = "sudo ip link set dev eth0 address 00:11:22:33:44:55";
            executeCommand(command3);

            // 修改网口2的MAC地址
            String command4 = "sudo ip link set dev eth1 address aa:bb:cc:dd:ee:ff";
            executeCommand(command4);

            // 设置网口1的网关
            String command5 = "sudo ip route add default via 192.168.1.1 dev eth0";
            executeCommand(command5);

            // 设置网口2的网关
            String command6 = "sudo ip route add default via 192.168.2.1 dev eth1";
            executeCommand(command6);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void executeCommand(String command) throws Exception {
        Process process = Runtime.getRuntime().exec(command);
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }
        process.waitFor();
    }

    public static void main(String[] args) {
        setIpAddress("eth0", "192.168.1.100/24"); // 修改网卡eth0的IP地址为192.168.1.100/24
        setMacAddress("eth0", "00:11:22:33:44:55"); // 修改网卡eth0的MAC地址为00:11:22:33:44:55
        setGateway("192.168.1.1"); // 设置默认网关为192.168.1.1
    }
}